#!/bin/usr/env  bash

swapoff -a
mkdir -p /var/log/k8s/


#master
# kube-apiserver
./kube-apiserver \
--logtostderr=true \
--v=4 \
--etcd-servers=http://172.18.101.24:2379 \
--insecure-bind-address=0.0.0.0 \
--insecure-port=8080 \
--service-cluster-ip-range=10.254.0.0/16 \
>> /var/log/k8s/kube-apiserver.log 2>&1 &

#kube-controller-manager
./kube-controller-manager \
--logtostderr=true \
--v=4 \
--master=172.18.101.24:8080 \
>> /var/log/k8s/kube-controller-manager.log 2>&1 &

#kube-scheduler
./kube-scheduler \
--logtostderr=true \
--v=4 \
--master=172.18.101.24:8080 \
>> /var/log/k8s/kube-scheduler.log 2>&1 &

#check etcd health
/data/workdir/etcd/etcd/etcdctl -C http://172.18.101.24:2379 cluster-health


#node
#kubelet

mkdir -p /var/lib/kubelet/
cat <<-EOF > /var/lib/kubelet/kubeconfig
apiVersion: v1
kind: Config
clusters:
  - cluster:
      server: http://172.18.101.24:8080/
    name: local
contexts:
  - context:
      cluster: local
    name: local
current-context: local
EOF


./kubelet \
--logtostderr=true \
--v=4 \
--address=0.0.0.0 \
--hostname-override=172.18.101.26 \
--kubeconfig /var/lib/kubelet/kubeconfig \
--cgroup-driver systemd \
>> /var/log/k8s/kubelet.log 2>&1 &

#kube-proxy
./kube-proxy \
--logtostderr=true \
--v=4 \
--hostname-override=172.18.101.26 \
--master=http://172.18.101.24:8080 \
>> /var/log/k8s/kube-proxy.log 2>&1 &


#验证
./kubectl -s http://172.18.101.24:8080 get componentstatuses 
./kubectl -s http://172.18.101.24:8080 get node


#all node
#flannel
/data/workdir/etcd/etcd/etcdctl -C http://172.18.101.24:2379 \
mk /coreos.com/network/config '{"Network":"10.0.0.0/16"}'

/data/workdir/etcd/etcd/etcdctl get /coreos.com/network/config

./flanneld \
-etcd-endpoints=http://172.18.101.24:2379 \
-etcd-prefix=/coreos.com/network \
>> /var/log/k8s/flanneld.log 2>&1 &

cat /run/flannel/subnet.env

FLANNEL_NETWORK=10.0.0.0/16
FLANNEL_SUBNET=10.0.37.1/24
FLANNEL_MTU=1472
FLANNEL_IPMASQ=false

vi /etc/sysconfig/docker-network
DOCKER_NETWORK_OPTIONS="--bip=10.0.16.1/24 --mtu=1472"

cat <<-EOF > /etc/docker/daemon.json  
{
 "insecure-registries" : ["myregistry.com","gcr.io"],
  "registry-mirrors": ["https://sbhkczb9.mirror.aliyuncs.com"]
}
EOF
echo "172.18.115.133 myregistry.com" >> /etc/hosts
echo "172.18.115.133 gcr.io" >> /etc/hosts

systemctl restart docker

# 测试网络
./kubectl -s http://172.18.101.24:8080 create -f  nginx-default-pod.yml
./kubectl -s http://172.18.101.24:8080 create -f  nginx-test-pod.yml
./kubectl -s http://172.18.101.24:8080 get all
./kubectl -s http://172.18.101.24:8080 describe pod nginx-default
./kubectl -s http://172.18.101.24:8080 describe pod nginx-pod

./kubectl -s http://172.18.101.24:8080 run my-nginx --image=nginx --replicas=2 --port=80
./kubectl -s http://172.18.101.24:8080 describe pod my-nginx-5d69b5ff7-rmcvg



#deploy dashboard
#参考: https://github.com/kubernetes/dashboard/releases
#     http://blog.csdn.net/liukuan73/article/details/78703632
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.8.0/src/deploy/recommended/kubernetes-dashboard.yaml
# 修改 --apiserver-host=http://172.18.101.24:8080
./kubectl  apply -f  kubernetes-dashboard.yaml
# 查看 api-server地址/ui
http://172.18.101.24:8080/ui
# 问题排查方法:
# 1. pod无法启动
# 查看pod分布在哪个节点上，然后查看var/log/kubelet.log的日志




#deploy skydns
# 参考: http://blog.csdn.net/zhuchuangang/article/details/76093887
#1. https://github.com/kubernetes/kubernetes/releases,下载对应的版本（1.8.5）
#2.解压
#3. 生成配置文件
cd  kubernetes/cluster/addons/dns/
cp kubedns-svc.yaml.sed kubedns-svc.yaml
sed -i 's/$DNS_SERVER_IP/10.254.10.2/g' kubedns-svc.yaml
cp kubedns-controller.yaml.sed kubedns-controller.yaml
sed -i 's/$DNS_DOMAIN/cluster.local/g' kubedns-controller.yaml
# 主机dns地址在10.254.0.0/16 之间，应为api server启动的时候已经指定了范围
# 执行模板
./kubectl create -f kubedns-cm.yaml
./kubectl create -f kubedns-svc.yaml
./kubectl create -f kubedns-controller.yaml
./kubectl create -f kubedns-sa.yaml
#修改kubelet的启动参数–cluster-dns=10.254.10.2 –cluster-domain=cluster.local
kill -9 $(ps -ef | grep  "kubelet" | grep -v grep | awk  '{print $2 }')

./kubelet \
--logtostderr=true \
--v=4 \
--address=0.0.0.0 \
--hostname-override=172.18.101.25 \
--kubeconfig /var/lib/kubelet/kubeconfig \
--cgroup-driver systemd \
--cluster-dns=10.254.10.2 \
--cluster-domain=cluster.local \
>> /var/log/k8s/kubelet.log 2>&1 &

./kubelet \
--logtostderr=true \
--v=4 \
--address=0.0.0.0 \
--hostname-override=172.18.101.26 \
--kubeconfig /var/lib/kubelet/kubeconfig \
--cgroup-driver systemd \
--cluster-dns=10.254.10.2 \
--cluster-domain=cluster.local \
>> /var/log/k8s/kubelet.log 2>&1 &
# 验证dns解析


